package kamikazehero.game.entity;

import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;

import javax.swing.Timer;

import net.java.games.input.Controller;
import net.java.games.input.Component;
import net.java.games.input.Event;
import net.java.games.input.EventQueue;
import net.java.games.input.Rumbler;

public class ControllableEntity implements Runnable{

	protected Controller controller;
	
	public int xCoord;
	public int yCoord;
	
	private float xTemp;
	private float yTemp;
	
	float[] xCompare = {0.0f, 0.0f};
	float[] yCompare = {0.0f, 0.0f};
	
	private int speedFactor = 2;
	
	private String axis = null;
	private DecimalFormat format = new DecimalFormat("0.0");
	
	private boolean isZAxis = false;
	private boolean isInverted;
	
	Component compx;
	Component compy;
	
	public ControllableEntity(Controller controller, Timer timer, int xStart, int yStart){
		
		this.controller = controller;

		
		this.xCoord = xStart;
		this.yCoord = yStart;
	
		this.xTemp = 0.0f;
		this.yTemp = 0.0f;
		
		timer.addActionListener(new pollAction());
	}
	
	private void poll(){
		
			controller.poll();
			
			if(!isZAxis){
				compx = controller.getComponent(net.java.games.input.Component.Identifier.Axis.X);
				compy = controller.getComponent(net.java.games.input.Component.Identifier.Axis.Y);	
			}else{
				compx = controller.getComponent(net.java.games.input.Component.Identifier.Axis.Z);
				compy = controller.getComponent(net.java.games.input.Component.Identifier.Axis.RZ);	
			}
			xCompare[0] = Float.parseFloat(format.format(compx.getPollData()));
			yCompare[0] = Float.parseFloat(format.format(compy.getPollData()));
					
				
	
			/*
					if(!isZAxis){
						if(axis.equals("x")  || axis.equals("X Axis")){
							xCompare[0] = Float.parseFloat(format.format(event.getValue()));
						}else if(axis.equals("y") || axis.equals("Y Axis")){
							yCompare[0] = Float.parseFloat(format.format(event.getValue()));
							
						}
					}else{
						if(axis.equals("z")  || axis.equals("Z Axis")){
							xCompare[0] = Float.parseFloat(format.format(event.getValue()));
						}else if(axis.equals("z rotation") || axis.equals("Z Rotation")){
							yCompare[0] = Float.parseFloat(format.format(event.getValue()));
							
						}
					}	
				//}
			//}
			*/
			xCompare[1] = xCompare[0];
			yCompare[1] = yCompare[0];		
	}
	
	private void translateInput(){
		
		if(xCompare[1] == -1.0f && yCompare[1] == 0.0f){
			xCoord += -1 - speedFactor;
		}else if(xCompare[1] == 1.0f && yCompare[1] == 0.0f){
			xCoord += 1 + speedFactor;
		}else if(xCompare[1] == 0.0f && yCompare[1] == -1.0f){
			yCoord += -1 - speedFactor;
		}else if(xCompare[1] == 0.0f && yCompare[1] == 1.0f){
			yCoord += 1 + speedFactor;
		}else{
			/*
			 * -x, -y
			 */
			if(xCompare[1] < 0.0f && yCompare[1] < 0.0f){
				if(xCompare[1] >= -1.0f){
					if(xCompare[1] == xCompare[0]){
						xTemp += xCompare[1];
					}
					if(xTemp <= -1.0f ){
						xCoord += (int)Math.ceil(xTemp) - speedFactor;
						xTemp = 0.0f;
					}
				}
				if(yCompare[1] >= -1.0f){
					if(yCompare[1] == yCompare[0]){
						yTemp += yCompare[1];
					}
				}
				if(yTemp <= -1.0f){
					yCoord += (int)Math.ceil(yTemp) - speedFactor;
					yTemp = 0.0f;
				}
			}
			
			/*
			 * x, -y
			 */
			
			if(xCompare[1] > 0.0f && yCompare[1] < 0.0f){
				if(xCompare[1] <= 1.0f){
					if(xCompare[1] == xCompare[0]){
						xTemp += xCompare[1];
	
					}
					if(xTemp >= 1.0f ){
						xCoord += (int)Math.floor(xTemp) + speedFactor;
						xTemp = 0.0f;
					}
				}
				if(yCompare[1] >= -1.0f){
					if(yCompare[1] == yCompare[0]){
						yTemp += yCompare[1];
					}
				}
				if(yTemp <= -1.0f){
					yCoord += (int)Math.ceil(yTemp) - speedFactor;
					yTemp = 0.0f;
				}
			}
			
			/*
			 * -x, y
			 */
			
			if(xCompare[1] < 0.0f && yCompare[1] > 0.0f){
				if(xCompare[1] >= -1.0f){
					if(xCompare[1] == xCompare[0]){
						xTemp += xCompare[1];
					}
					if(xTemp <= -1.0f ){
						xCoord += (int)Math.ceil(xTemp) - speedFactor;
						xTemp = 0.0f;
					}
				}
				if(yCompare[1] <= 1.0f){
					if(yCompare[1] == yCompare[0]){
						yTemp += yCompare[1];
					}
				}
				if(yTemp >= 1.0f){
					yCoord += (int)Math.floor(yTemp) + speedFactor;
					yTemp = 0.0f;
				}
			}
			
			/*
			 * x, y
			 */
			
			if(xCompare[1] > 0.0f && yCompare[1] > 0.0f){
				if(xCompare[1] <= 1.0f){
					if(xCompare[1] == xCompare[0]){
						xTemp += xCompare[1];
	
					}
					if(xTemp >= 1.0f ){
						xCoord += (int)Math.floor(xTemp) + speedFactor;
						xTemp = 0.0f;
					}
				}
				if(yCompare[1] <= 1.0f){
					if(yCompare[1] == yCompare[0]){
						yTemp += yCompare[1];
					}
				}
				if(yTemp >= 1.0f){
					yCoord += (int)Math.floor(yTemp) + speedFactor;
					yTemp = 0.0f;
				}
			}
		}
	}

	public boolean isZAxis() {
		return isZAxis;
	}

	public void setZAxis(boolean isZAxis) {
		this.isZAxis = isZAxis;
	}

	public boolean isInverted() {
		return isInverted;
	}

	public void setInverted(boolean isInverted) {
		this.isInverted = isInverted;
	}

	@Override
	public void run() {
		
	}
	
	public class pollAction implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			poll();
			translateInput();
		}
	}
}
